#include <QVector3D>
#include <QDebug>
#include "geometry.h"

float distanceBetween(const QVector3D &point, const Ray &ray) {
    QVector3D v1 = ray.m_point - point;
    QVector3D v2 = (ray.m_point + ray.m_vector) - point;

    // v1, v2的叉积的长度值是这两个矢量构成的平行四边形的面积
    float area = QVector3D::crossProduct(v1,v2).length();
    // 面积除以底边长等于高，即点到射线的距离
    float r = area / ray.m_vector.length();
    return r;
}

QVector3D intersect(Plan &plan, Ray &ray) {
    QVector3D ray2PlanVector = plan.m_point - ray.m_point;
    // ray的起点到平面的距离
    float distance1 = QVector3D::dotProduct(ray2PlanVector, plan.m_normal);
    // ray的方向矢量在平面法线上的投影长度
    float distance2 = QVector3D::dotProduct(ray.m_vector, plan.m_normal);
    // 射线法向量的缩放系数
    float scale = distance1 / distance2;
    // 射线与平面的交点
    return ray.m_point + scale * ray.m_vector;
}
